package com.ks.system.controller;


import com.ks.handler.BusinessException;
import com.ks.response.Result;
import com.ks.response.ResultCode;
import com.ks.system.entity.Department;
import com.ks.system.service.DepartmentService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author ks
 * @since 2020-12-01
 */
@Api(value = "部门管理",tags = "系统部门信息接口")
@RestController
@RequestMapping("/system/department")
@CrossOrigin
public class DepartmentController {

    @Autowired
    private DepartmentService departmentService;

    @GetMapping("/findDepCount")
    @ApiOperation(value = "查询部门信息及人数",notes = "根据部门表查询部门信息及部门人数")
    public Result findDepCount(){

        /**
         * 调用serve层方法查询部门数据
         */
        List<Department> depCount = departmentService.findDepCount();

        /*进行判断是否查询到部门数据*/
        if(depCount.size() == 0){
            /*进入此判断代表未查询到部门数据 抛出部门不存在为空指针异常*/
            throw new BusinessException(ResultCode.DEPARTMENT_NOT_EXIST.getCode(),
                    ResultCode.DEPARTMENT_NOT_EXIST.getMessage());
        }else{
            return Result.ok().data("depCount",depCount);
        }
    }
}

